﻿var visterList=new Object();
//var visterList=new Array();
var visterUniqID=0;

function AddVisterUser(client,copId){
	client.copId=copId;
	
	if(visterList[copId]==undefined){
		visterList[copId]=new Array();
	}
	
	var clientObj=new Object();
	clientObj.client=client;
	clientObj.ID=visterUniqID;
	clientObj.nickname="Vister["+visterList[copId].length.toString()+"]";
	clientObj.tacked=false;
	
	visterList[copId].push(clientObj);
	visterUniqID++;
	application.acceptConnection(client);
	
	//updata vister informations
	var info=new Object();
	info.nickname=clientObj.nickname;
	info.companyid=copId;
	info.id=clientObj.ID;
	info.copId=copId;
	
	CallClient(client,"InitUserInfos",info);
	//updata vister water line info
	CallClient(client,"QueueInfo",visterList[copId].length-1);
	//------------------------------------------
	
	//call All Accounts about to updata there asking list
	AC_BroadcastCopMsg(copId,"VisterAskingList",GetVisterNameList(copId));
	
	
}
//del vister from list
function DelVisterUser(client){
	var copId=client.copId;
	if(visterList[copId]!=undefined){
		for(var i=0;i<visterList[copId].length;i++){
			if(visterList[copId][i].client==client){
				trace("Del Vister:["+visterList[copId][i].nickname+"]");
				visterList[copId].splice(i,1);
			}
		}
	}
	//call All Accounts about to updata there asking list
	AC_BroadcastCopMsg(copId,"VisterAskingList",GetVisterNameList(copId));
	VT_UpdataQueueInfoCop(copId);
}
//Broadcast msg to Vister
function VT_BroadcastCopMsg(copId,FunName,infos){
	if(visterList[copId]!=undefined){
		for(var i=0;i<visterList[copId].length;i++){
			var c=visterList[copId][i].client;
			c.call(FunName,null,msg);
		}
	}
}
//updata vister queue info
function VT_UpdataQueueInfoCop(copId){
	if(visterList[copId]!=undefined){
		for(var i=0;i<visterList[copId].length;i++){
			var c=visterList[copId][i].client;
			c.call("QueueInfo",null,i);
		}
	}
}
// get visiter name-list with id
function GetVisterNameList(copId){
	var ar=[];
	if(visterList[copId]!=undefined){
		for(var i=0;i<visterList[copId].length;i++){
			ar.push({
				name:visterList[copId][i].nickname,
				id:visterList[copId][i].ID,
				tacked:visterList[copId][i].tacked
				});
		}
	}
	//trace("COP ID:"+copId)
	//MyTrace(ar);
	return ar;
}
// get vister object
function GetVister(copId,id){
	//trace("get vister:"+copId+"|"+id);
	if(visterList[copId]!=undefined){
		for(var i=0;i<visterList[copId].length;i++){
			//trace(visterList[copId][i].ID+"=="+id+" = "+(visterList[copId][i].ID==id));
			if(visterList[copId][i].ID==id){
				return visterList[copId][i];
			}
		}
	}else{
		trace("not find COP.");
	}
	return null;
}